#include "opedb.h"

OpeDB::OpeDB(QObject *parent) : QObject(parent)
{

    //要操作的数据库
    m_db = QSqlDatabase::addDatabase("QSQLITE");
}

OpeDB &OpeDB::getInstance()
{
    static OpeDB instance;
    return instance;
}

void OpeDB::init()
{
    //数据库主机IP地址
    m_db.setHostName("localhost");

    //数据库名称
    m_db.setDatabaseName("D:\\_08_qt\\_00_pros\\cloud\\TcpServer\\cloud.db");

    //打开数据库
    if(m_db.open())
    {
        //数据查询测试
        QSqlQuery query;
        query.exec("select * from usrInfo");
        while (query.next())
        {
            QString data = QString("%1, %2, %3").arg(query.value(0).toString()).arg(query.value(1).toString()).arg(query.value(2).toString());
            qcout << data;
        }
    }
    else
    {
       qcout<< "数据库打开失败";
    }
}

OpeDB::~OpeDB()
{
    //数据库使用完毕关闭数据库
    qcout<< "关闭数据库";
    m_db.close();
}

//注册操作, 向数据库userInfo表中插入数据
bool OpeDB::handleRegist(const QString name, const QString pwd)
{
    if(name.isEmpty()|| pwd.isEmpty())
    {
        qcout << "handleRegist false! name or pwd is empty";
        return false;
    }
    QString data = QString("insert into usrInfo(name , pwd) values(\'%1\',\'%2\');").arg(name).arg(pwd);

    QSqlQuery query;
    if(query.exec(data))//执行
    {
        return true;
    }
    return false;
}

//处理客户端登录操作, 在数据库userInfo表中查询登录信息
int OpeDB::handleLogin(const QString name, const QString pwd)
{
    if(name.isEmpty()|| pwd.isEmpty())
    {
        qcout << "handleLogin false! name or pwd is empty";
        return ENUM_LOGIN_ERROR_EMPTY_TYPE;
    }
    QString data = QString("select * from usrInfo where name = \'%1\'").arg(name);
    QSqlQuery query;
    if(query.exec(data) && query.next())//执行
    {
        //查询到了结果, 继续匹配密码和在线状态
        //if(query.next())
        {
            //查询到了数据
            qcout<<"query.next()...";
            QString userPwd = query.value(2).toString();
            QString userOnline = query.value(3).toString();
            if(userPwd == pwd)//密码匹配正确
            {
                qcout << "密码正确";
                if(userOnline == "0")//用户也是不在线的, 可以把用户的状态更改为在线状态了
                {
                    qcout << "用户也不在线";
                    data = QString("update usrInfo set online = 1 where name=\'%1\';").arg(name);

                    if(query.exec(data))
                    {
                        qcout << "更新了用户" << name << "为在线状态";
                    }
                    else
                    {
                        qcout << "未成功更新用户" << name << "状态";
                    }
                    return ENUM_LOGIN_ERROR_ALLOW_TYPE;
                }
                else
                {
                    return ENUM_LOGIN_ERROR_ONLINE_TYPE;//返回用户已在线错误码
                }
            }
            else
            {
                return ENUM_LOGIN_ERROR_PWD_TYPE;//返回密码错误码
            }

        }
    }
    else
    {
        qcout << "未查询到该用户名, 用户名查询错误";
        //未查询到结果, 返回错误码
        return ENUM_LOGIN_ERROR_NAME_TYPE;
    }

}

//服务器处理数据库用户下线请求
bool OpeDB::handleOffLine(const QString name)
{
    if(name.isEmpty())
    {
        qcout << "handleOffLine false! name is empty";
        return false;
    }

    QString data = QString("update usrInfo set online = 0 where name=\'%1\';").arg(name);
    QSqlQuery query;
    if(query.exec(data))
    {
        qcout << "更新了用户" << name << "为离线状态";
        return true;
    }
    else
    {
        qcout << "未成功更新用户" << name << "状态";
        return false;
    }


}

//服务器处理客户端查看所有在线用户请求
QStringList OpeDB::handleAllOnline()
{
    QString data = QString("select name from usrInfo where online=1;");
    QStringList usrList ;
    QSqlQuery query;
    if(query.exec(data))
    {
        qcout << "服务器数据库查询操作成功, 查询到所有用户集";

        usrList.clear();
        while(query.next())
        {
            usrList.append(query.value(0).toString());
        }
    }
    return usrList;
}

//处理客户端搜索用户操作, 在数据库userInfo表中查询要搜索的用户信息
int OpeDB::handleSearchUsr(const QString name)
{
    QString data = QString("select * from usrInfo where name = \'%1\'").arg(name);
    QSqlQuery query;
    if(query.exec(data) && query.next())//执行
    {
        //搜索的用户存在
        //两个状态
        //1.在线
        //2.离线
        QString userOnline = query.value(3).toString();
        if(userOnline == "0")
        {
            //用户也是不在线的
            return ENUM_SEARCH_OFFLINE_TYPE;
        }
        else
        {
            //用户在线, 返回状态码
            return ENUM_SEARCH_ONLINE_TYPE;
        }

    }
    else
    {
        //搜素的用户不存在, 返回错误码
        return ENUM_SEARCH_ERROR_NAME_TYPE;
    }
}

//处理客户端添加好友操作, 在数据库userInfo表中查询好友信息
//返回值结果
//  return ENUM_ADDFRIEND_TYPE_EMPTY;//用户名或密码为空
//  return ENUM_ADDFRIEND_TYPE_ALWAYS;//查询到结果表明两个人已经是好友了
//  return ENUM_SEARCH_OFFLINE_TYPE;//两一个人暂时不在线, 暂时不能添加好友
//  return ENUM_ADDFRIEND_TYPE_SUCCESS;//可以添加好友
int OpeDB::handleAddFriend(const QString firstName, const QString secondName)
{
    if(firstName.isEmpty() || secondName.isEmpty())
    {
        //用户名或密码为空
        qcout << "用户名或密码为空";
        return ENUM_ADDFRIEND_TYPE_EMPTY;
    }
    else
    {
        qcout << "检查中...";
        /**
         * select * from friend where (
         *                              id = ( select id from usrInfo where name = 'jack')
         *                              and
         *                              friendId = (select id from usrInfo where name = 'lucy')
         *                             )
         *                             or
         *                             (
         *                              friendId = (select * from usrInfo where name = 'jack')
         *                              and
         *                              id = ( select id from usrInfo where name = 'lucy')
         *                             )
         *
        **/

        QString data = QString("select * from friend where (  "
                               "id = ( select id from usrInfo where name = \'%1\')     "
                               "and                                                    "
                               "friendId = (select id from usrInfo where name = \'%2\')"
                               ")                                                       "
                               "or                                                      "
                               "(                                                       "
                               "friendId = (select * from usrInfo where name = \'%2\') "
                               "and                                                    "
                               "id = ( select id from usrInfo where name = \'%1\')     "
                               ")");


        QSqlQuery query;
        if(query.exec(data) && query.next())//执行
        {
            //查询到结果表明两个人已经是好友了
            qcout << firstName<< "和" << secondName <<"已经是好友了";
            return ENUM_ADDFRIEND_TYPE_ALWAYS;
        }
        else
        {
            //查询到两个人没有关系, 可以成为好友
            qcout << firstName<< "和" << secondName <<"可以成为好友";

            if(handleSearchUsr(secondName) == ENUM_SEARCH_OFFLINE_TYPE)
            {
                qcout << "sName 离线, 暂时不能添加";
                //用户离线, 不能添加好友
                return ENUM_SEARCH_OFFLINE_TYPE;
            }
            else
            {
                qcout << "sName 在线, 准备添加";
                //用户在线, 且之前也不是好友, 就可以发送添加好友请求
                return ENUM_ADDFRIEND_TYPE_SUCCESS;
            }

        }

    }
}


